package com.mishow.activity.photo;

import java.util.ArrayList;
import java.util.List;

import com.mishow.R;
import com.mishow.activity.DynamicReleaseActivity;
import com.mishow.activity.SelectReleaseActivity;
import com.mishow.activity.base.BaseActivity;
import com.mishow.adapter.photo.AlbumGridViewAdapter;
import com.mishow.bean.photo.ImageBucket;
import com.mishow.bean.photo.ImageItem;
import com.mishow.utils.Constants;
import com.mishow.utils.photo.AlbumHelper;
import com.mishow.utils.photo.Bimp;
import com.mishow.utils.photo.PublicWay;
import com.mishow.utils.photo.Res;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
/**
 * 作者：wei.miao <br/>
 * 描述：本地选择照片
 */
public class AlbumActivity extends BaseActivity {

	/**
	 * 显示手机里的所有图片的列表控件
	 */
	private GridView gridView;
	/**
	 * 当手机里没有图片时，提示用户没有图片的控件
	 */
	private TextView tv;
	/**
	 * gridView的adapter
	 */
	private AlbumGridViewAdapter gridImageAdapter;
	/**
	 * 完成按钮
	 */
	private Button okButton;
	/**
	 * 返回按钮
	 */
	private Button back;
	/**
	 * 取消按钮
	 */
	private Button cancel;
	private Intent intent;
	/**
	 * 预览按钮
	 */
	private Button preview;
	private Context mContext;
	private ArrayList<ImageItem> dataList;
	private AlbumHelper helper;
	public static List<ImageBucket> contentList;
	public static Bitmap bitmap;
	
	private int release_type;

	@Override
	protected void initData() {
		mContext = this;
		helper = AlbumHelper.getHelper();
		helper.init(getApplicationContext());

		contentList = helper.getImagesBucketList(false);
		dataList = new ArrayList<ImageItem>();
		for (int i = 0; i < contentList.size(); i++) {
			dataList.addAll(contentList.get(i).imageList);
		}

		// 注册一个广播，这个广播主要是用于在GalleryActivity进行预览时，防止当所有图片都删除完后，再回到该页面时被取消选中的图片仍处于选中状态
		IntentFilter filter = new IntentFilter("data.broadcast.action");
		registerReceiver(broadcastReceiver, filter);
		bitmap = BitmapFactory.decodeResource(getResources(),
				Res.getDrawableID("plugin_camera_no_pictures"));

		intent = getIntent();
		Bundle bundle = intent.getExtras();
		release_type = bundle.getInt(Constants.IMAGE_RELEASE_TYPE);
	}

	@Override
	protected void initView(Bundle savedInstanceState) {

		setContentView(Res.getLayoutID("plugin_camera_album"));
		PublicWay.activityList.add(this);

		back = (Button) findViewById(Res.getWidgetID("back"));
		cancel = (Button) findViewById(Res.getWidgetID("cancel"));
		preview = (Button) findViewById(Res.getWidgetID("preview"));
		gridView = (GridView) findViewById(Res.getWidgetID("myGrid"));
		tv = (TextView) findViewById(Res.getWidgetID("myText"));
		okButton = (Button) findViewById(Res.getWidgetID("ok_button"));

	}

	@Override
	protected void setAttribute() {

		init();

		initListener();
		// 这个函数主要用来控制预览和完成按钮的状态
		isShowOkBt();
	}

	private void init() {

		cancel.setOnClickListener(new CancelListener());
		back.setOnClickListener(new BackListener());
		preview.setOnClickListener(new PreviewListener());
		gridImageAdapter = new AlbumGridViewAdapter(this, dataList,Bimp.tempSelectBitmap);
		gridView.setAdapter(gridImageAdapter);

		gridView.setEmptyView(tv);

		okButton.setText(Res.getString("finish") + "("
				+ Bimp.tempSelectBitmap.size() + "/" + PublicWay.num + ")");

	}

	private void initListener() {

		gridImageAdapter
				.setOnItemClickListener(new AlbumGridViewAdapter.OnItemClickListener() {

					@Override
					public void onItemClick(final ToggleButton toggleButton,
							int position, boolean isChecked, Button chooseBt) {
						if (Bimp.tempSelectBitmap.size() >= PublicWay.num) {
							toggleButton.setChecked(false);
							chooseBt.setVisibility(View.GONE);
							if (!removeOneData(dataList.get(position))) {
								Toast.makeText(AlbumActivity.this,
										Res.getString("only_choose_num"), 200)
										.show();
							}
							return;
						}
						if (isChecked) {
							chooseBt.setVisibility(View.VISIBLE);
							Bimp.tempSelectBitmap.add(dataList.get(position));
							okButton.setText(Res.getString("finish") + "("
									+ Bimp.tempSelectBitmap.size() + "/"
									+ PublicWay.num + ")");
						} else {
							Bimp.tempSelectBitmap.remove(dataList.get(position));
							chooseBt.setVisibility(View.GONE);
							okButton.setText(Res.getString("finish") + "("
									+ Bimp.tempSelectBitmap.size() + "/"
									+ PublicWay.num + ")");
						}
						isShowOkBt();
					}
				});

		okButton.setOnClickListener(new AlbumSendListener());

	}

	private boolean removeOneData(ImageItem imageItem) {
		if (Bimp.tempSelectBitmap.contains(imageItem)) {
			Bimp.tempSelectBitmap.remove(imageItem);
			okButton.setText(Res.getString("finish") + "("
					+ Bimp.tempSelectBitmap.size() + "/" + PublicWay.num + ")");
			return true;
		}
		return false;
	}

	public void isShowOkBt() {
		if (Bimp.tempSelectBitmap.size() > 0) {
			okButton.setText(Res.getString("finish") + "("
					+ Bimp.tempSelectBitmap.size() + "/" + PublicWay.num + ")");
			preview.setPressed(true);
			okButton.setPressed(true);
			preview.setClickable(true);
			okButton.setClickable(true);
			okButton.setTextColor(Color.WHITE);
			preview.setTextColor(Color.WHITE);
		} else {
			okButton.setText(Res.getString("finish") + "("
					+ Bimp.tempSelectBitmap.size() + "/" + PublicWay.num + ")");
			preview.setPressed(false);
			preview.setClickable(false);
			okButton.setPressed(false);
			okButton.setClickable(false);
			okButton.setTextColor(Color.parseColor("#E1E0DE"));
			preview.setTextColor(Color.parseColor("#E1E0DE"));
		}
	}

	BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			gridImageAdapter.notifyDataSetChanged();
		}
	};

	/**
	 * 
	 * 作者：wei.miao <br/>
	 * 描述：预览按钮的监听
	 */
	private class PreviewListener implements OnClickListener {
		public void onClick(View v) {
			if (Bimp.tempSelectBitmap.size() > 0) {
				intent.putExtra("position", "1");
				intent.setClass(AlbumActivity.this, GalleryActivity.class);
				startActivity(intent);
			}
		}

	}

	/**
	 * 作者：wei.miao <br/>
	 * 描述：完成按钮的监听
	 */
	private class AlbumSendListener implements OnClickListener {
		public void onClick(View v) {
			overridePendingTransition(R.anim.activity_translate_in,R.anim.activity_translate_out);
			if (release_type == Constants.IMAGE_RELEASE_TYPE_DYNAMIC) {
				intent.setClass(mContext, DynamicReleaseActivity.class);
			}else if (release_type == Constants.IMAGE_RELEASE_TYPE_SELECT){
				intent.setClass(mContext, SelectReleaseActivity.class);
			}
			startActivity(intent);
			finish();
		}

	}

	/**
	 * 返回按钮监听
	 */
	private class BackListener implements OnClickListener {
		public void onClick(View v) {
			intent.setClass(AlbumActivity.this, ImageFile.class);
			startActivity(intent);
		}
	}

	/**
	 * 取消按钮的监听
	 */
	private class CancelListener implements OnClickListener {
		public void onClick(View v) {
			Bimp.tempSelectBitmap.clear();
			if (release_type == Constants.IMAGE_RELEASE_TYPE_DYNAMIC) {
				intent.setClass(mContext, DynamicReleaseActivity.class);
			}else if (release_type == Constants.IMAGE_RELEASE_TYPE_SELECT){
				intent.setClass(mContext, SelectReleaseActivity.class);
			}
			startActivity(intent);
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			intent.setClass(AlbumActivity.this, ImageFile.class);
			intent.putExtra(Constants.IMAGE_RELEASE_TYPE,release_type);
			startActivity(intent);
		}
		return false;
	}

	@Override
	protected void onRestart() {
		isShowOkBt();
		super.onRestart();
	}

}
